package net.chasing.androidbaseconfig.widget.view_pager;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;

import net.chasing.androidbaseconfig.R;
import net.chasing.androidbaseconfig.decoration.VerticalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerIndicator extends RecyclerView {
    private ViewPagerIndicatorAdapter mAdapter;
    private VerticalDividerItemDecoration mDividerDecoration;

    public ViewPagerIndicator(@NonNull Context context) {
        super(context);
        init();
    }

    public ViewPagerIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ViewPagerIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mAdapter = new ViewPagerIndicatorAdapter(getContext());
        setAdapter(mAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        setLayoutManager(linearLayoutManager);
        mDividerDecoration = new VerticalDividerItemDecoration.Builder(getContext())
                .visibilityProvider((position, parent) -> {
                    if (parent.getAdapter() != null)
                        return position == parent.getAdapter().getItemCount() - 1;
                    return false;
                }).size(getContext().getResources().getDimensionPixelSize(R.dimen.indicator_decoration)).build();
        addItemDecoration(mDividerDecoration);
    }

    public void setViewPager(@NonNull ViewPager vp) {
        if (vp.getAdapter() != null) {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < vp.getAdapter().getCount(); i++)
                list.add("");
            mAdapter.insertAll(list);
        }
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                mAdapter.setSelectPosition(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    // region 设置属性
    public void setDividerDecoration(int size) {
        removeItemDecoration(mDividerDecoration);
        mDividerDecoration = new VerticalDividerItemDecoration.Builder(getContext())
                .visibilityProvider((position, parent) -> {
                    if (parent.getAdapter() != null)
                        return position == parent.getAdapter().getItemCount() - 1;
                    return false;
                }).size(size).build();
        addItemDecoration(mDividerDecoration);
    }

    public void setSize(int size) {
        mAdapter.setSize(size);
    }

    public void setSelectColor(int color) {
        mAdapter.setSelectColor(color);
    }

    public void setUnSelectColor(int color) {
        mAdapter.setUnSelectColor(color);
    }
    // endregion
}
